import { Effect } from 'dva';
import { Reducer } from 'redux';
import { produce, Draft } from 'immer';
import { setToken } from '@/utils/cookie';
import { getUserInfo } from '@/services/user';
import { getDictType, getDept } from '@/services/dict';
import { Moment } from 'moment';

export interface UserInfo {
  createBy?: string;
  createTime?: string | Moment;
  // dept (SysDept, optional): 部门表 ,
  // params (object, optional),
  // postIds (Array[integer], optional),
  // posts (Array[DictType对象], optional),
  remark?: string;
  // roleIds (Array[integer], optional),
  // roles (Array[Role对象], optional),
  searchValue?: string;
  surAvatar?: string; // 用户头像 ,
  surBeginTime?: string | Moment; // 开始时间 ,
  surCreateBy?: string; // 创建人 ,
  surCreateTime?: string | Moment; // 创建时间 ,
  surDelFlag?: string; // 删除标志（0代表存在 2代表删除） ,
  surDeptId?: number; // 部门编号 ,
  surDeptName?: string;
  surEmail?: string; // 用户邮箱 ,
  surEndTime?: string | Moment; // 结束时间 ,
  surIsAdmin?: number; // 是否管理员,0是,1否 ,
  surLoginName?: string; // 登录名称 ,
  surPassword?: string; // 密码 ,
  surPhoneNumber?: string; // 手机号码 ,
  surRemark?: string; // 备注 ,
  surRoleId?: number; //
  surSalt?: string; // 盐加密 ,
  surSex?: string; // 用户性别0=男,1=女,2=未知 ,
  surStatus?: string; // 帐号状态（0正常 1停用） ,
  surSystemId?: number; // 系统平台ID ,
  surSystemKey?: string; // 系统平台KEY ,
  surUpdateBy?: string; // 修改人 ,
  surUpdateTime?: string | Moment; // 修改时间 ,
  surUserId?: number; // 用户ID ,
  surUserName?: string; // 用户昵称 ,
  surUserType?: string; // 类型:Y默认用户,N非默认用户 ,
  updateBy?: string;
  updateTime?: number; //
}
export interface WsMsgItem{
  code: string;
  data: string;
}
export interface UserModelState {
  user: UserInfo;
  wsMessages: WsMsgItem[];
}

export interface UserModelType {
  namespace?: string;
  state: UserModelState;
  effects: {
    getUser: Effect;
    addWsMessage: Effect;
    deleteWsMessage: Effect;
  };
  reducers: {
    changeUserStatus: Reducer;
    changeAddWsMessage: Reducer;
    changeDelWsMessage: Reducer;
  };
}

const Model: UserModelType = {
  namespace: 'user',

  state: {
    user: {},
    wsMessages: [],
  },

  effects: {
    *getUser({}, { call, put }) {
      let [err, data] = yield call(getUserInfo);
      localStorage.setItem('surSystemKey',data.surSystemKey);
      localStorage.setItem('surSystemId',data.surSystemId);

      if(!err) {
        yield put({
          type: 'changeUserStatus',
          payload: data,
        });
      }
      // 设置字典数据
      [err, data] = yield call(getDictType, { dictType: 'brand' });
      localStorage.setItem('brandDict', err ? '[]' : JSON.stringify(data));

      [err, data] = yield call(getDictType, { dictType: 'unitName' });
      localStorage.setItem('unitNameDict', err ? '[]' : JSON.stringify(data));

      [err, data] = yield call(getDept, { sdtStatus: '0' });
      localStorage.setItem('deptDict', err ? '[]' : JSON.stringify(data));
    },
    *addWsMessage({ payload }, {call, put}) {
      yield put({
        type: 'changeAddWsMessage',
        payload,
      });
    },
    *deleteWsMessage({ }, {call, put}) {
      yield put({
        type: 'changeDelWsMessage',
      });
    },
  },

  reducers: {
    changeUserStatus: produce((draft: Draft<UserModelState>, { payload }): void => {
      draft.user = payload;
    }),
    changeAddWsMessage: produce((draft: Draft<UserModelState>, { payload }): void => {
      draft.wsMessages.pop();
      draft.wsMessages.push(payload);
    }),
    changeDelWsMessage: produce((draft: Draft<UserModelState>): void => {
      draft.wsMessages.pop();
    }),
  },
};

export default Model;
